package com.privatbank.servlets;

import com.privatbank.Dao.GoodDao;
import com.privatbank.constants.Constants;
import com.privatbank.tables.Good;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by Slava on 27.07.2014.
 */
public class GoodsListServlet extends HttpServlet {

    private GoodDao dao = null;

    @Override
    public void init() {
        dao = new GoodDao(Constants.LOGIN, Constants.PASSWORD, Constants.URL);
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        init();
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        pw.println("<B>Список товаров</B>");
        pw.println("<table border=1>");
        try {
            pw.println("<tr>");
            pw.println("<th>ID_manufacturer</th>");
            pw.println("<th>Model</th>");
            pw.println("<th>ID_category</th>");
            pw.println("<th>Description</th>");
            pw.println("<th>Price</th>");
            pw.println("<th>Image</th>");
            pw.println("<th>Storage</th>");
            pw.println("</tr>");
            List<Good> l = dao.getAll();
            for (Good us : l) {
                pw.println("<tr>");
                pw.println("<td>" + us.getIdManufacturer() + "</td>");
                pw.println("<td>" + us.getModel() + "</td>");
                pw.println("<td>" + us.getIdCategory() + "</td>");
                pw.println("<td>" + us.getDescription() + "</td>");
                pw.println("<td>" + us.getPrice() + "</td>");
                pw.println("<td>" + us.getImage() + "</td>");
                pw.println("<td>" + us.getStorage() + "</td>");
                pw.println("</tr>");
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
        pw.println("</table>");
    }
}
